package com.tinyblog.ajax2;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.*;

public class BlogManager extends HttpServlet {
	private static final long serialVersionUID = -7392062830558685106L;
	/**
	 * 服务总入口
	 */
	public void service (HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
		String action = "";
		if(request.getParameter("action")!=null) action = request.getParameter("action");
		if("viewComment".equals(action))
			this.getAllComment(request, response);
		else if("viewReference".equals(action))
			this.getAllReference(request, response);
		else if("addComment".equals(action))
			this.addComment(request, response);
		else if("viewEntry".equals(action))
			this.getEntry(request, response);
		else if("viewList".equals(action))
			this.getList(request, response);
		else if("updateItem".equals(action))
			this.updateListItem(request, response);
		else if("addItem".equals(action))
			this.addItem(request, response);
		else if("deleteItem".equals(action))
			this.deleteListItem(request, response);
	}
	/**
	 * 获取全部评论，并输出为XML文档
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void getAllComment(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String blogid = request.getParameter("blogid");
		BlogService service = new BlogService();
		PrintWriter out = response.getWriter();
		try {
			response.setContentType("application/xml");
			service.outputComment(out, blogid);
		}catch(Exception ex) {
			this.handleError(ex);
		}
	}
	/**
	 * 获取全部引用通告，并输出为XML文档
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void getAllReference(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String blogid = request.getParameter("blogid");
		BlogService service = new BlogService();
		PrintWriter out = response.getWriter();
		try {
			response.setContentType("application/xml");
			service.outputReference(out,blogid);
		}catch(Exception ex) {
			this.handleError(ex);
		}
	}
	/**
	 * 添加评论
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void addComment(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String blogid = request.getParameter("blogid");
		BlogService service = new BlogService();
		BlogComment comment = new BlogComment();
		if(request.getParameter("author")!=null)
			comment.setAuthor(request.getParameter("author"));
		if(request.getParameter("blogimage")!=null)
			comment.setBlogimage(request.getParameter("blogimage"));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		comment.setCommentdate(sdf.format(new Date()));
		if(request.getParameter("blogurl")!=null)
			comment.setBlogurl(request.getParameter("blogurl"));
		if(request.getParameter("email")!=null)
			comment.setEmail(request.getParameter("email"));
		if(request.getParameter("commentcontent")!=null)
			comment.setCommentcontent(request.getParameter("commentcontent"));
		try {
			service.addComment(comment, blogid);
		}catch(Exception ex) {
			this.handleError(ex);
		}
		String strForward = "../msnspace/blogManager?action=viewComment&blogid="+blogid;
		this.forward(strForward, request, response);
	}
	/**
	 * 获取日志的固定链接
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void getEntry(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
		String blogid = request.getParameter("blogid");
		BlogService service = new BlogService();
		PrintWriter out = response.getWriter();
		try {
			Blog blog = service.getBlog(blogid);
			response.setContentType("text/html");
			out.print(blog.getEntry());
		}catch(Exception ex) {
			this.handleError(ex);
		}
	}
	/**
	 * 获取指定的列表及其列表项
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void getList(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String listid = request.getParameter("listid");
		BlogService service = new BlogService();
		List listes = new ArrayList();
		PrintWriter out = response.getWriter();
		try {
			BlogList list = service.getBlogList(listid);
			listes.add(list);
			response.setContentType("application/xml");
			service.outputList(out, (BlogList[])listes.toArray(new BlogList[0]));
		}catch(Exception ex) {
			this.handleError(ex);
		}
	}
	/**
	 * 更新指定的列表项
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void updateListItem(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String listid = request.getParameter("listid");
		String itemid = request.getParameter("itemid");
		BlogService service = new BlogService();
		try {
			BlogListItem item = service.getListItem(listid, itemid);
			if(request.getParameter("itemname")!=null)
				item.setItemname(request.getParameter("itemname"));
			if(request.getParameter("itemurl")!=null);
				item.setItemurl(request.getParameter("itemurl"));
			if(request.getParameter("description")!=null)
				item.setDescription(request.getParameter("description"));
			service.updateListItem(listid, item);
			String strForward = "../msnspace/blogManager?action=viewList&listid="+listid;
			this.forward(strForward, request, response);
		}catch(Exception ex) {
			this.handleError(ex);
		}
	}
	/**
	 * 添加新的列表项
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void addItem(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String listid = request.getParameter("listid");
		BlogService service = new BlogService();
		try {
			BlogListItem item = new BlogListItem();
			if(request.getParameter("itemname")!=null)
				item.setItemname(request.getParameter("itemname"));
			if(request.getParameter("itemurl")!=null);
				item.setItemurl(request.getParameter("itemurl"));
			if(request.getParameter("description")!=null)
				item.setDescription(request.getParameter("description"));
			service.addListItem(listid, item);
			String strForward = "../msnspace/blogManager?action=viewList&listid="+listid;
			this.forward(strForward, request, response);
		}catch(Exception ex) {
			this.handleError(ex);
		}
	}
	/**
	 * 删除指定的列表项
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void deleteListItem(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String listid = request.getParameter("listid");
		String itemid = request.getParameter("itemid");
		BlogService service = new BlogService();
		try {
			service.deleteListItem(listid, itemid);
			String strForward = "../msnspace/blogManager?action=viewList&listid="+listid;
			this.forward(strForward, request, response);
		}catch(Exception ex) {
			this.handleError(ex);
		}
	}
	/**
	 * 错误处理
	 * @param ex
	 */
	private void handleError(Exception ex) {
		ex.printStackTrace();
		System.out.println(ex.toString());
	}
    /**
     * 处理页面转向
     * @param strForward
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void forward(String strForward, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		RequestDispatcher dispatcher = request.getRequestDispatcher(strForward);
		dispatcher.forward((ServletRequest)request, (ServletResponse)response);
	}
}
